package com.gougou.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

//    private <T> T execute(Function<T,Jedis> fun){
//        Jedis resource=null;
//        try {
//            resource = jedisPool.getResource();
//            return fun.callback(resource);
//        }finally {
//            if (resource!=null){
//                resource.close();
//            }
//        }
//    }



    /*************************
     *
     * 向redis数据库中存入key=value
     *
     * @param key
     * @param value
     * @return
     * ***********************
     */
    public String set(String key ,String value){
        Jedis resource=null;
        String set=null;
        try {
            resource = jedisPool.getResource();
            resource.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (resource!=null){
                resource.close();
            }
        }
        return set;
    }

    /**************************
     *
     * 从redis数据库钟得到值
     *  传入key 得到    value
     *
     * @param key
     * @return
     * ***********************
     */
    public String get(String key){
        Jedis resource=null;
        String value="";
        try {
            resource = jedisPool.getResource();
            value = resource.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (resource!=null){
                resource.close();
            }
        }
        return value;
    }
    /**************************
     *
     * 从redis数据库删除数据 （key)
     *
     * @param key
     * @return
     * ***********************
     */
    public long del(String key){
        Jedis resource=null;
        Long value=null;
        try {
            resource = jedisPool.getResource();
            value = resource.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (resource!=null){
                resource.close();
            }
        }
        return value;
    }


    /**************************
     *
     * 设置redis的key的生存时间
     *
     * @param key
     * @return
     * ***********************
     */
    public long expire(String key,Integer seconds){
        Jedis resource=null;
        Long value=null;
        try {
            resource = jedisPool.getResource();
            return resource.expire(key,seconds);
        }finally {
            if (resource!=null){
                resource.close();
            }
        }
    }

    /**************************
     *
     * 插入redis 一个key值为value并设置key的生存时间为seconds
     *
     * @param key
     * @return
     * ***********************
     */
    public long set(String key,String value,Integer seconds){
        Jedis resource=null;
        try {
            resource = jedisPool.getResource();
            resource.set(key,value);
            return resource.expire(key,seconds);
        }finally {
            if (resource!=null){
                resource.close();
            }
        }
    }




}
